class Solution {
public:
    vector<vector<string>> ans;
    bool col[10] = {false};
    bool dig1[20] = {false};
    bool dig2[20] = {false};
    void dfs(int pos, int n, vector<vector<int>>& vis) {
        if (pos >= n) {
            vector<string> x;
            for (int i = 0; i < n; i++) {
                string s;
                for (int j = 0; j < n; j++) {
                    if (vis[i][j] == 1)
                        s += "Q";
                    else
                        s += ".";
                }
                x.push_back(s);
            }
            ans.push_back(x);
            return;
        }
        for (int i = 0; i < n; i++) {
            if (vis[pos][i] == 0) {
                if (!col[i] && !dig1[i - pos + n] && !dig2[i + pos]) {
                    vis[pos][i] = 1;
                    col[i] = dig1[i - pos + n] = dig2[i + pos] = 1;
                    dfs(pos + 1, n, vis);
                    col[i] = dig1[i - pos + n] = dig2[i + pos] = 0;
                    vis[pos][i] = 0;
                }
            }
        }
    }
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<int>> vis(n, vector<int>(n, 0));
        dfs(0, n, vis);
        return ans;
    }
};